package com.nenglai.common.crud;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@ToString
@NoArgsConstructor
public class SearchWrapper<T> {

    private SearchUtil searchUtil;

    /**
     * page, pagesize
     */
    private int page = 1;
    private int pagesize = 15;
    /**
     * 查询条件
     */
    private List<JSONObject> conditions;
    /**
     * 排序
     */
    private List<JSONObject> sort;
    /**
     * 对象封装操作类
     */
    @JsonIgnore
    private QueryWrapper<T> query = new QueryWrapper<>();
    /**
     * 实体类型字典
     */
    @JsonIgnore
    private Map<String, Class<?>> parallel;

    public SearchWrapper<T> query(Class<?> clazz) {
        this.init(clazz);
        return this;
    }

    /**
     * 初始化（实体类型字典）
     *
     * @param clazz
     * @return void
     * @author kongdehui
     * @date 2021/04/21 18:45
     */
    private void init(Class<?> clazz) {
        Map<String, Class<?>> parallel = new HashMap<>();
        Field[] fields = clazz.getDeclaredFields();
        Field field;
        for (int i = 0; i < fields.length; i++) {
            try {
                field = fields[i];
                field.setAccessible(true); // 开启访问权限
                parallel.put(field.getName(), field.getType());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        this.parallel = parallel;
    }

    /**
     * 条件查询
     *
     * @return com.xiaositea.admin.kernel.crud.SearchWrapper<T>
     * @author kongdehui
     * @date 2021/04/21 20:13
     */
    public SearchWrapper<T> where() {
        if (conditions != null) {
            for (JSONObject next : conditions) {
                searchUtil.condition(query, next, parallel);
            }
        }
        return this;
    }

    /**
     * 排序
     *
     * @return com.xiaositea.admin.kernel.crud.SearchWrapper<T>
     * @author kongdehui
     * @date 2021/04/21 20:23
     */
    public SearchWrapper<T> orderBy() {
        if (sort == null) return this;
        for (JSONObject next : sort) {
            searchUtil.sort(query, next);
        }
        return this;
    }

    /**
     * pagination
     *
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<T>
     * @author kongdehui
     * @date 2021/04/21 20:26
     */
    public Page<T> page() {
        return new Page<>(this.page, this.pagesize);
    }

    /**
     * pagination
     *
     * @param pagesize
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<T>
     * @author kongdehui
     * @date 2021/04/21 20:26
     */
    public Page<T> page(Integer pagesize) {
        return new Page<T>(1, pagesize);
    }
}

